package com.siburian.MyStuff;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class Profil_Stat {
	private static final String ID_STATISTIC = "ID_Statistic";
	private static final String ROW_ID = "ID_Profil";
	private static final String ID_KELAS = "ID_Kelas";
	private static final String ID_PELAJARAN = "ID_Pelajaran";
	private static final String ID_BAB = "ID_Bab";
	private static final String SKOR = "Skor";
	 
	private static final String NAMA_DB = "ProfileStatistik";// nama database
	private static final String NAMA_TABEL = "ProfileStat";// nama tabel
	private static final int DB_VERSION = 1;
 
	private static final String CREATE_TABLE_PROFILE = "create table "+NAMA_TABEL+" ("+ID_STATISTIC+" integer PRIMARY KEY autoincrement,"+ROW_ID+" integer, "+ID_KELAS+" integer,"+ID_PELAJARAN+" integer,"+ID_BAB+" integer,"+SKOR+" integer)";
	private final Context context;
	private DatabaseOpenHelper dbHelper;
	private SQLiteDatabase db;
 
	public Profil_Stat(Context ctx) {
		this.context = ctx;
		dbHelper = new DatabaseOpenHelper(context);
		db = dbHelper.getWritableDatabase();
	}
 
	private static class DatabaseOpenHelper extends SQLiteOpenHelper {
		 
		public DatabaseOpenHelper(Context context) {
			super(context, NAMA_DB, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_TABLE_PROFILE);
		}
 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS "+NAMA_TABEL);
			onCreate(db);
 
		}
	}
 
	public void close() {
		dbHelper.close();
	}
 
	public void addProfile(int id_profil, int id_kelas,int id_pelajaran,int id_bab,int skor) {
 
		ContentValues values = new ContentValues();
		values.put(ROW_ID, id_profil);
		values.put(ID_KELAS, id_kelas);
		values.put(ID_PELAJARAN, id_pelajaran); 
		values.put(ID_BAB, id_bab);
		values.put(SKOR, skor);
		try {
 
		//	db.delete(NAMA_TABEL, null, null);
			db.insert(NAMA_TABEL, null, values);
		} catch (Exception e) {
			Log.e("DB ERROR", e.toString());
			e.printStackTrace();
		}
	}
	
	
	public ArrayList<ArrayList<Object>> getProfile(int profil,int kelas,int pelajaran,int bab){
		ArrayList<ArrayList<Object>> dataArray = new ArrayList<ArrayList<Object>>();
		Cursor cur;
		try {
			cur = db.query(NAMA_TABEL,
					new String[] { ROW_ID,ID_KELAS, ID_PELAJARAN,ID_BAB,SKOR }, ROW_ID + "=" + '"' + profil + '"'+ " and " + ID_PELAJARAN+ "=" + '"' + pelajaran + '"'+ " and " + ID_KELAS+ "=" + '"' + kelas + '"' + " and " + ID_BAB+ "=" + '"' + bab + '"', null,
					null, null, null);
			cur.moveToFirst();
			if (!cur.isAfterLast()) {
				do {
					ArrayList<Object> dataList = new ArrayList<Object>();
					dataList.add(cur.getLong(0));
					dataList.add(cur.getString(1));
					dataList.add(cur.getString(2));
					dataList.add(cur.getString(3));
					dataList.add(cur.getString(4));
					dataArray.add(dataList);
 
				} while (cur.moveToNext());
 
			}
 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e("DEBE ERROR", e.toString());
		}
		return dataArray;
	}
}
